extends Node

@export var mob_scene: PackedScene

var score

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	pass

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta: float) -> void:
	pass

func _on_mob_timer_timeout() -> void:
	spawn_mobs()

func _on_score_timer_timeout() -> void:
	score += 1
	$HUD.update_score(score)

func _on_player_died() -> void:
	game_over()

func new_game():
	$Player.show_at($Marker2D.position)
	
	score = 0
	$HUD.update_score(score)
	
	$MobTimer.start()
	$scoreTimer.start()

func game_over():
	$MobTimer.stop()
	$scoreTimer.stop()
	
	$HUD.show_restart()

func spawn_mobs():
	var location = $MobPath/MobSpawnLocation
	location.progress_ratio = randf()
	
	var d = location.rotation
	d += PI/2
	d += randf_range(-PI/4, PI/4)
	
	var x = randf_range(150.0, 200.0)
	var v = Vector2(x, 0.0).rotated(d)
	
	var mob = mob_scene.instantiate()
	mob.position = location.position
	mob.rotation = d
	mob.linear_velocity = v
	
	add_child(mob)
